package com.hgy.fxui.dao;

import com.hgy.fxui.model.Supplier;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface SupplierDao {
    /**
     * 获取供应商地区分布统计
     */
    @Select("SELECT address as region, COUNT(*) as count FROM supplier GROUP BY address ORDER BY count DESC")
    List<Map<String, Object>> getSupplierRegionDistribution();

    /**
     * 获取合作时间最长的前N个供应商
     */
    @Select("SELECT name, create_time FROM supplier ORDER BY create_time ASC LIMIT #{limit}")
    List<Map<String, Object>> getOldestSuppliers(int limit);

    /**
     * 获取供应商总数量
     */
    @Select("SELECT COUNT(*) FROM supplier")
    int getTotalSuppliersCount();

    /**
     * 获取近30天新增的供应商数量
     */
    @Select("SELECT COUNT(*) FROM supplier WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)")
    int getNewSuppliersCount();

    @Select("SELECT * FROM supplier")
    List<Supplier> getAllSuppliers();

    @Insert("INSERT INTO supplier (name, contact_person, phone, address, create_time) VALUES (#{name}, #{contactPerson}, #{phone}, #{address}, now())")
    int insert(Supplier supplier);
    int update(Supplier supplier);

    @Delete("DELETE FROM supplier WHERE id = #{id}")
    int delete(Long id);
}
